package com.zhangch.base.hutool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.junit.Test;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 功能说明: <br>
 * 系统版本: 1.0 <br>
 * 开发人员: zhanch
 * 开发时间: 2021/3/9<br>
 * <br>
 */
public class IMSAndHrDbTest {

	public static void main(String[] args) {

	}

	@Test
	public void queryTest() throws SQLException {
		String sql_2 = "update SMSUSER.b_salesemp_info set emp_state = '%s' where  emp_code  = '%s' and emp_state = '%s';";
		String sql_3 = "update sso_account set state = '%s' where  account  = '%s' and state = '%s';";
		String basePath_1 = "C:\\Users\\11326\\Desktop\\FileRead - 副本\\";
		String destpath = basePath_1 + File.separator + "账户中心状态更新"+new Date().getTime() +".sql";
		String destpathIms = basePath_1 + File.separator + "IMS账户状态更新"+new Date().getTime() +".sql";

		File file = new File(destpath);
		FileUtil.del(file);
		File fileIms = new File(destpathIms);
		FileUtil.del(fileIms);
		List<Entity> hrFind = Db.use("hr").query("select account,nick_name,state from sso_account where deleted = 0 and create_user = 'lpwang01' ");
		Map<String, Entity> hrMap = hrFind.stream()
				.collect(Collectors.toMap(entiy -> (String) entiy.get("account"), Function.identity()));
		CollUtil.split(new ArrayList<>(hrMap.keySet()),1000).forEach(list ->{
			List<Entity> imsFind = null;
			try {
				list = list.stream().map(str -> "\'"+str+"\'").collect(Collectors.toList());

				//imsFind = Db.use("ims").query("select emp_state from SMSUSER.b_salesemp_info where emp_code in ('hzongm01')");
				imsFind = Db.use("ims").query("select emp_code,emp_state from SMSUSER.b_salesemp_info where emp_code in ("+StrUtil.join(",",list)+")");
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
			Map<String, Entity> imsMap = imsFind.stream()
					.collect(Collectors.toMap(entiy -> (String) entiy.get("emp_code"), Function.identity()));
			imsMap.keySet().forEach(emp_code ->{
				Entity entity = hrMap.get(emp_code);
				if(entity != null && noEqual(entity,imsMap.get(emp_code))){
					if("D".equals(entity.get("state"))){
						String sql = String.format(sql_2,"N",emp_code,imsMap.get(emp_code).get("emp_state"));
						FileUtil.appendUtf8String(sql,fileIms);
						FileUtil.appendUtf8String("\n", fileIms);
					}else{
						String sql = String.format(sql_3,"D",emp_code,entity.get("state"));
						FileUtil.appendUtf8String(sql,file);
						FileUtil.appendUtf8String("\n", file);
					}
				}
			});

		});



	}

	private boolean noEqual(Entity entity, Entity _entity) {
		String state = (String) entity.get("state");
		String emp_state = (String) _entity.get("emp_state");
		return (!state.equals(emp_state) && state.equals("Y") && emp_state.equals("N"))
				|| (!state.equals(emp_state) && state.equals("D") && emp_state.equals("Y"));
	}

	private String part_1(){
		StringBuilder stringBuilder = new StringBuilder();
		IntStream.range(0, 1000).forEach(index ->{
			if(index == 99){
				stringBuilder.append("?");
			}else {
				stringBuilder.append("?,");
			}
		});
		return stringBuilder.toString();
	}
}
